package com.yjs.ppjokedemo.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yjs.ppjokedemo.R;
import com.yjs.ppjokedemo.adapter.FraAdapter;
import com.yjs.ppjokedemo.bean.CustomEntity;
import com.yjs.ppjokedemo.ui.find.LikedFragment;
import com.yjs.ppjokedemo.ui.find.RecommendFragment;

import java.util.ArrayList;


public class FindFragment extends Fragment {


    private CommonTabLayout tabFind;
    private ViewPager vpFind;

    FraAdapter mFraAdapter;
    public FindFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_find, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View View) {
        tabFind = (CommonTabLayout) View.findViewById(R.id.tab_find);
        vpFind = (ViewPager) View.findViewById(R.id.vp_find);

        ArrayList<CustomTabEntity> tabs = new ArrayList<>();
        tabs.add(new CustomEntity("关注",0,0));
        tabs.add(new CustomEntity("推荐",0,0));
        tabFind.setTabData(tabs);

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new LikedFragment());
        fragments.add(new RecommendFragment());
        mFraAdapter = new FraAdapter(getChildFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,fragments);
        vpFind.setAdapter(mFraAdapter);

        vpFind.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabFind.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tabFind.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vpFind.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }
}